import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '首页', icon: 'el-icon-s-home', affix: true }
    }]
  },
  // 发布管理
  {
    path: '/publish',
    component: Layout,
    redirect: '/publish/article',
    name: 'Publish',
    meta: { title: '发布管理', icon: 'el-icon-edit-outline' },
    children: [
      {
        path: 'article',
        name: 'Article',
        component: () => import('@/views/article/index'),
        meta: { title: '文章管理', icon: 'el-icon-tickets' }
      },
      {
        path: 'aphorism',
        name: 'Aphorism',
        component: () => import('@/views/aphorism/index'),
        meta: { title: '随机诗语', icon: 'el-icon-loading' }
      },
      {
        path: 'category',
        name: 'Category',
        component: () => import('@/views/category/index'),
        meta: { title: '分类管理', icon: 'el-icon-menu' }
      },
      {
        path: 'comment',
        name: 'Comment',
        component: () => import('@/views/comment/index'),
        meta: { title: '留言管理', icon: 'el-icon-edit' }
      },
      {
        path: 'reply',
        name: 'Reply',
        component: () => import('@/views/reply/index'),
        meta: { title: '回复管理', icon: 'el-icon-chat-dot-round' }
      },
      {
        path: 'message',
        name: 'Message',
        component: () => import('@/views/message/index'),
        meta: { title: '私信管理', icon: 'el-icon-message' }
      },
      {
        path: 'background',
        name: 'Background',
        component: () => import('@/views/background/index'),
        meta: { title: '背景图片', icon: 'el-icon-picture' }
      }
    ]
  },

  // 赞助管理
  {
    path: '/help',
    component: Layout,
    redirect: '/patron/patron',
    name: 'Help',
    meta: { title: '赞助管理', icon: 'el-icon-coin' },
    children: [
      {
        path: 'patron',
        name: 'Patron',
        component: () => import('@/views/patron/index'),
        meta: { title: '赞助1元', icon: 'el-icon-medal-1' }
      },
      {
        path: 'sponsor',
        name: 'Sponsor',
        component: () => import('@/views/sponsor/index'),
        meta: { title: 'VIP赞助', icon: 'el-icon-s-custom' }
      },
      {
        path: 'looper',
        name: 'Looper',
        component: () => import('@/views/looper/index'),
        meta: { title: '投图赞助', icon: 'el-icon-picture' }
      }
    ]
  },
  // 系统管理
  {
    path: '/system',
    component: Layout,
    redirect: '/system/system',
    name: 'System',
    meta: { title: '系统管理', icon: 'el-icon-setting' },
    children: [
      {
        path: 'user',
        name: 'User',
        component: () => import('@/views/user/index'),
        meta: { title: '用户管理', icon: 'el-icon-user' }
      },
      {
        path: 'role',
        name: 'Role',
        component: () => import('@/views/role/index'),
        meta: { title: '角色管理', icon: 'el-icon-user-solid' }
      },
      {
        path: 'menu',
        name: 'Menu',
        component: () => import('@/views/menu/index'),
        meta: { title: '菜单管理', icon: 'el-icon-s-grid' }
      },
      {
        path: 'announcement',
        name: 'Announcement',
        component: () => import('@/views/announcement/index'),
        meta: { title: '系统公告', icon: 'el-icon-microphone' }
      }
    ]
  },
  // 系统通知
  {
    path: '/notification',
    component: Layout,
    redirect: '/notification',
    name: 'Notification',
    meta: { title: '系统通知', icon: 'el-icon-setting' },
    children: [
      {
        path: 'award',
        name: 'Award',
        component: () => import('@/views/award/index'),
        meta: { title: '用户奖励', icon: 'el-icon-user' }
      },
      {
        path: 'notice',
        name: 'Notice',
        component: () => import('@/views/notice/index'),
        meta: { title: '所有通知', icon: 'el-icon-bell' }
      }
    ]
  },
  // 处理举报
  {
    path: '/audit',
    component: Layout,
    redirect: '/audit/',
    name: 'Audit',
    meta: { title: '举报管理', icon: 'el-icon-s-check' },
    children: [
      {
        path: 'report',
        name: 'Report',
        component: () => import('@/views/report/index'),
        meta: { title: '举报管理', icon: 'el-icon-wind-power' }
      }
    ]
  },
  // 系统监控
  {
    path: '/safety',
    component: Layout,
    redirect: '/monitor/monitor.js',
    name: 'Safety',
    meta: { title: '系统监护', icon: 'el-icon-s-platform' },
    children: [
      {
        path: 'monitor',
        name: 'Monitor',
        component: () => import('@/views/monitor/index'),
        meta: { title: '系统监护', icon: 'el-icon-s-platform' }
      }
    ]
  },
  {
    path: 'external-link',
    component: Layout,
    children: [
      {
        path: 'http://localhost:3000/',
        meta: { title: '经历官网', icon: 'el-icon-s-promotion' }
      }
    ]
  },
  /**
   * 标签导航刷新路由
   */
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  mode: 'history', // require service support 开启历史路由
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
